/****************************************************************************
 * 2023.12 超萌大脑斧
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace QFramework.Example
{
	/// <summary>
	/// 升级界面，当经验满了升级时弹出
	/// </summary>
	public partial class ExpUpgradePanel : UIElement,IController
	{
		private ResLoader mResLoader;
		
		private void Awake()
		{
			mResLoader = ResLoader.Allocate(); // 加载资源
			var iconAtlas = mResLoader.LoadSync<SpriteAtlas>("icon");
			//var simpleKnifeIcon = iconAtlas.GetSprite("simple_knife_icon");
			
			// 获取经验升级系统
			var expUpgradeSystem = this.GetSystem<ExpUpgradeSystem>();

			// 隐藏掉技能升级Item的预设体
			ExpUpgradeItemTemplate.Hide();
			// 遍历所有的技能升级Item，生成对应的技能升级Item展示
			foreach (var expUpgradeItem in expUpgradeSystem.Items)
			{
				//print(expUpgradeItem.Visible.Value);
				ExpUpgradeItemTemplate.InstantiateWithParent(UpgradeRoot)
					.Self(self =>
					{
						var itemCache = expUpgradeItem; //要做缓存，不然最后（itemCache.Upgrade()）调用的事件就是遍历最后一个按钮的事件了
						self.transform.Find("Icon").GetComponent<Image>().sprite = iconAtlas.GetSprite(itemCache.IconName); //设置图标
						//print(self.name);
						// 选择某项能力升级后，游戏继续运行
						self.onClick.AddListener(() =>
						{
							Time.timeScale = 1.0f;
							itemCache.Upgrade();
							this.Hide();
							AudioKit.PlaySound("ability_level_up");
						});

						var selfCache = self;
						selfCache.Hide();

						// 每个能力的Visble都注册事件，当Visible改变时触发
						itemCache.Visible.RegisterWithInitValue(visible =>
						{
							if (visible)
							{
								self.GetComponentInChildren<Text>().text = expUpgradeItem.Description;
								selfCache.Show();

								var pairedUpgradeName = selfCache.transform.Find("PairedUpgradeName");
								// 设置配对信息
								if (expUpgradeSystem.Pairs.TryGetValue(itemCache.Key,out var pairedName))
								{
									var pairedItem = expUpgradeSystem.Dictionary[pairedName];
									if (pairedItem.CurrentLevel.Value>0&&itemCache.CurrentLevel.Value==0)
									{
										//var pairedNameText=selfCache.transform.Find("PairedUpgradeName");
										//pairedUpgradeName.GetComponent<Text>().text = "配对技能：" + pairedItem.Key;
										pairedUpgradeName.Find("Icon").GetComponent<Image>().sprite =
											iconAtlas.GetSprite(pairedItem.PairedIconName);
										pairedUpgradeName.Show();
									}
									else
									{
										pairedUpgradeName.Hide(); 
									}
								}
								else
								{
									pairedUpgradeName.Hide(); 
								}
							}
							else
							{
								selfCache.Hide();
							}
						}).UnRegisterWhenGameObjectDestroyed(selfCache);

						itemCache.CurrentLevel.RegisterWithInitValue(lv =>
						{
							selfCache.GetComponentInChildren<Text>().text = itemCache.Description;
						}).UnRegisterWhenGameObjectDestroyed(gameObject);
					});
			}



			//游戏开始时升级按钮要隐藏
			// BtnUpgrade.onClick.AddListener(() =>
			// {
			// 	// 继续游戏
			// 	Time.timeScale = 1.0f;
			// 	// 每次升级如果选择升级攻击力，则攻击力越高
			// 	Global.SimpleAbilityDamage.Value *= 1.5f;
			// 	UpgradeRoot.Hide();
			// 	AudioKit.PlaySound("ability_level_up");
			// });
			//
			// BtnSimpleDurationUpgrade.onClick.AddListener(() =>
			// {
			// 	// 继续游戏
			// 	Time.timeScale = 1.0f;
			// 	// 每次升级如果选择升级攻击频率，则攻击频率越快
			// 	Global.SimpleAbilityDuration.Value *= 0.8f;
			// 	UpgradeRoot.Hide();
			// 	AudioKit.PlaySound("ability_level_up");
			// });
		}

		protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}